package com.uzai.console.web.jd;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.device.DeviceQuery;
import com.uzai.console.dto.jd.jdorder.*;
import com.uzai.console.dto.jd.jdtoken.JdTokenQuery;
import com.uzai.console.dto.tb.tborder.TbOrderDto;
import com.uzai.console.dto.user.UserQuery;
import com.uzai.console.entity.JdToken;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.JdTokenMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.device.DeviceTypeService;
import com.uzai.console.service.jd.JdCategoryService;
import com.uzai.console.service.user.UserService;
import com.uzai.console.service.wechatprivate.DeviceWechatInfoService;
import com.uzai.console.service.jd.JdOrderService;
import com.uzai.console.service.order.OrderReStatusService;
import com.uzai.console.service.order.OrderSourceService;
import com.uzai.console.service.order.OrderStatusService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.device.DeviceInfoVo;
import com.uzai.console.vo.jd.jdorder.JdCategoryVo;
import com.uzai.console.vo.tb.tborder.TbCategoryVo;
import com.uzai.console.vo.user.UserInfoVo;
import com.uzai.console.vo.jd.jdorder.JdOrderInfoVo;
import com.uzai.console.vo.jd.jdorder.JdOrderReportVo;
import com.uzai.console.vo.order.OrderReStatusVo;
import com.uzai.console.vo.order.OrderSourceVo;
import com.uzai.console.vo.order.OrderStatusVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "京东订单",tags = "京东|京东订单")
@ApiSort(10)
@RestController
public class JdOrderController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(JdOrderController.class);

    @Autowired
    private JdOrderService jdOrderService;
    @Autowired
    private OrderStatusService orderStatusService;
    @Autowired
    private OrderReStatusService orderReStatusService;
    @Autowired
    private OrderSourceService orderSourceService;
    @Autowired
    private JdTokenMapper jdTokenMapper;
    @Autowired
    private DeviceWechatInfoService deviceWechatInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private JdCategoryService jdCategoryService;


    @ApiOperation(value = "查询京东订单列表", notes = "查询京东订单列表", response = JdOrderInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/jdOrder/findJdOrderList")
    public Object findJdOrderList(@RequestBody JdOrderQuery jdOrderQuery){
        return jdOrderService.findJdOrderList(jdOrderQuery);
    }

    @ApiOperation(value = "查询京东订单详情", notes = "查询京东订单详情", response = JdOrderInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/jdOrder/findJdOrderInfo")
    public Object findJdOrderInfo(@RequestBody JdOrderIdDto jdOrderIdDto){
        return jdOrderService.findJdOrderInfo(jdOrderIdDto);
    }

    @ApiOperation(value = "查询京东订单统计信息(总收入、买家总金额、总利润、未绑定用户利润、未收货返利金额）", notes = "查询京东订单统计信息(总收入、买家总金额、总利润、未绑定用户利润、未收货返利金额）", response = JdOrderReportVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/jdOrder/findJdOrderReport")
    public Object findJdOrderReport(@RequestBody JdOrderQuery jdOrderQuery){
        return jdOrderService.findJdOrderReport(jdOrderQuery);
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "订单删除", notes = "订单删除")
    @ApiOperationSupport(order = 4)
    @PostMapping("/jdOrder/delJdOrder")
    public Object delJdOrder(@RequestBody JdOrderDelDto jdOrderDelDto){
        return jdOrderService.delJdOrder(jdOrderDelDto);
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "确认收货", notes = "确认收货")
    @ApiOperationSupport(order = 5)
    @PostMapping("/jdOrder/confirmReceive")
    public Object confirmReceive(@RequestBody JdOrderIdDto jdOrderIdDto){
        return jdOrderService.confirmReceive(jdOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "手动维权", notes = "手动维权")
    @ApiOperationSupport(order = 6)
    @PostMapping("/jdOrder/confirmRefund")
    public Object confirmRefund(@RequestBody JdOrderIdDto jdOrderIdDto){
        return jdOrderService.confirmRefund(jdOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "订单冻结", notes = "订单冻结")
    @ApiOperationSupport(order = 7)
    @PostMapping("/jdOrder/confirmFrozen")
    public Object confirmFrozen(@RequestBody JdOrderIdDto jdOrderIdDto){
        return jdOrderService.confirmFrozen(jdOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "订单解冻", notes = "订单解冻")
    @ApiOperationSupport(order = 8)
    @PostMapping("/jdOrder/confirmThaw")
    public Object confirmThaw(@RequestBody JdOrderIdDto jdOrderIdDto){
        return jdOrderService.confirmThaw(jdOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "解绑用户", notes = "解绑用户")
    @ApiOperationSupport(order = 9)
    @PostMapping("/jdOrder/unbindUser")
    public Object unBindUser(@RequestBody JdOrderUnbindUserDto jdOrderUnbindUserDto){
        return jdOrderService.unbindUser(jdOrderUnbindUserDto);
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "绑定用户", notes = "绑定用户")
    @ApiOperationSupport(order = 10)
    @PostMapping("/jdOrder/bindUser")
    public Object bindUser(@RequestBody JdOrderBindUserDto jdOrderBindUserDto){
        return jdOrderService.bindUser(jdOrderBindUserDto);
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改解冻时间", notes = "修改解冻时间")
    @ApiOperationSupport(order = 11)
    @PostMapping("/jdOrder/updateThawTime")
    public Object updateThawTime(@RequestBody JdOrderThawTimeDto jdOrderThawTimeDto){
        return jdOrderService.updateThawTime(jdOrderThawTimeDto);
    }

    @ApiOperation(value = "查询联盟账号接口", notes = "查询联盟账号接口")
    @ApiOperationSupport(order = 12)
    @PostMapping("/jdOrder/findAlimmList")
    public Object findAlimmList(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        JdTokenQuery jdTokenQuery = new JdTokenQuery();
        jdTokenQuery.setMerId(getLoginInfo().getMerId());
        List<JdToken> jdTokenList = jdTokenMapper.findByList(jdTokenQuery);
        if(jdTokenList != null){
            for (JdToken jdToken : jdTokenList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(jdToken.getId()));
                selectKeyVo.setValue(jdToken.getUnionId());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.DOWNLOAD)
    @ApiOperation(value = "手动下载京东订单", notes = "手动下载京东订单")
    @ApiOperationSupport(order = 13)
    @PostMapping("/jdOrder/downloadOrder")
    public Object downloadOrder(@RequestBody JdOrderDownLoadDto jdOrderDownLoadDto){
        jdOrderService.downloadOrder(jdOrderDownLoadDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.DOWNLOAD)
    @ApiOperation(value = "订单刷新", notes = "订单刷新")
    @ApiOperationSupport(order = 14)
    @PostMapping("/jdOrder/refreshOrder")
    public Object refreshOrder(@RequestBody JdOrderIdDto jdOrderIdDto){
        jdOrderService.refreshOrder(jdOrderIdDto);
        return true;
    }

    @ApiOperation(value = "查询所有未绑定用户订单", notes = "查询所有未绑定用户订单", response = JdOrderInfoVo.class)
    @ApiOperationSupport(order = 15)
    @PostMapping("/jdOrder/findUnbindUserList")
    public Object findUnbindUserList(@RequestBody JdOrderUnbindDtoQuery jdOrderUnbindDtoQuery){
        return jdOrderService.findUnbindUserList(jdOrderUnbindDtoQuery);
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除所有未绑定用户订单", notes = "删除所有未绑定用户订单")
    @ApiOperationSupport(order = 16)
    @PostMapping("/jdOrder/deleteUnbindUserList")
    public Object deleteUnbindUserList(@RequestBody JdOrderDeleteUnbindDto jdOrderDeleteUnbindDto){
        jdOrderDeleteUnbindDto.setDelStatus(1); //1表示已删除
        jdOrderDeleteUnbindDto.setUpdateTime(DateUtil.getNowTime());
        return jdOrderService.deleteUnbindUserList(jdOrderDeleteUnbindDto);
    }

    @ApiOperation(value = "查询京东订单状态列表", notes = "查询京东订单状态列表", response = OrderStatusVo.class)
    @ApiOperationSupport(order = 17)
    @PostMapping("/jdOrder/getOrderStatusList")
    public Object getOrderStatusList() {
        return orderStatusService.findJdOrderStatusList();
    }

    @ApiOperation(value = "查询京东订单返利状态列表", notes = "查询京东订单返利状态列表", response = OrderReStatusVo.class)
    @ApiOperationSupport(order = 18)
    @PostMapping("/jdOrder/getOrderReStatusList")
    public Object getOrderReStatusList() {
        return orderReStatusService.findOrderReStatusList();
    }

    @ApiOperation(value = "查询京东订单来源列表", notes = "查询京东订单订单来源列表", response = OrderSourceVo.class)
    @ApiOperationSupport(order = 19)
    @PostMapping("/jdOrder/getOrderSourceList")
    public Object getOrderSourceList() {
        return orderSourceService.findOrderSourceList();
    }

    @ApiOperation(value = "查询机器人下拉列表", notes = "查询机器人下拉列表", response = DeviceInfoVo.class)
    @ApiOperationSupport(order = 20)
    @PostMapping("/jdOrder/findDeviceWechatInfoSelectList")
    public Object findDeviceWechatInfoSelectList() {
        DeviceQuery deviceQuery = new DeviceQuery();
        deviceQuery.setMerId(getLoginInfo().getMerId());
        return deviceService.findDeviceList(deviceQuery);
    }

    @ApiOperation(value = "查询所有未绑定用户统计信息(总收入、买家总金额、总利润）", notes = "查询所有未绑定用户订单统计信息(总收入、买家总金额、总利润）", response = JdOrderReportVo.class)
    @ApiOperationSupport(order = 21)
    @PostMapping("/jdOrder/findUnbindUserReport")
    public Object findUnbindUserReport(@RequestBody JdOrderUnbindDtoQuery jdOrderQuery){
        return jdOrderService.findUnbindUserReport(jdOrderQuery);
    }

    @ApiOperation(value = "分页查询会员信息列表", notes = "分页查询会员信息列表", response = UserInfoVo.class)
    @ApiOperationSupport(order = 22)
    @PostMapping("/jdOrder/findUserPage")
    public Object findUserPage(@RequestBody UserQuery userQuery) {
        return userService.findUserPage(userQuery);
    }

    @ApiOperation(value = "查询设备类型下拉列表", notes = "查询设备类型下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 23)
    @PostMapping("/jdOrder/findDeiviceTypeListSelect")
    public Object findDeiviceTypeListSelect() {
        return deviceTypeService.findDeviceTypeListSelect();
    }

    @SystemLogType(model = SystemModelEnum.JdOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "追回返利", notes = "追回返利")
    @ApiOperationSupport(order = 24)
    @PostMapping("/jdOrder/rebateRefound")
    public Object rebateRefound(@RequestBody JdOrderIdDto jdOrderIdDto){
        return jdOrderService.rebateRefound(jdOrderIdDto);
    }

    @ApiOperation(value = "查询京东类目列表", notes = "查询京东类目列表", response = JdCategoryVo.class)
    @ApiOperationSupport(order = 25)
    @PostMapping("/jdcategory/all")
    public Object jdcategoryAll() {
        return jdCategoryService.findJdCategoryList();
    }
}
